
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.List;
import java.util.Vector;
import javax.swing.plaf.synth.Region;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultTreeCellEditor;

public class Base {

    //<editor-fold defaultstate="collapsed" desc="Atributos">
    public Connection c;
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Construtor">
    public Base() {
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Métodos">
    /*
     * Método genérico para conectar a base de dados benessere.
     */
    public void conectarBanco() {
        try {
            String url = "jdbc:mysql://localhost:3306/db_benessere?user=root&password=";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            c = (Connection) DriverManager.getConnection(url);
        } catch (Exception e) {
        }
    }

    /*
     * Metodo para buscar o ultimo id inserido na tabela é incrementado nas
     * classes de cadastro.
     */
    public int buscarProximoID(String sql, String id) {
        try {
            Statement st = (Statement) c.createStatement();
            ResultSet rs = st.executeQuery(sql);
            System.out.println(sql);
            while (rs.next()) {
                return Integer.parseInt(rs.getString("max(" + id + ")")) + 1;
            }
        } catch (Exception e) {
        }
        return 0;
    }
    /*
     * Método para buscar id de clinicas
     */

    public int buscarID(String sql, String id) {
        try {
            Statement st = (Statement) c.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                return rs.getInt(id);
            }
        } catch (Exception e) {
        }
        return 0;
    }


    /*
     * Método genérico para cadastro, exclusao, alteração apenas muda a sql
     * recebida
     */
    public boolean cadastrar(String sql) {
        try {
            Statement st = (Statement) c.createStatement();
            st.executeUpdate(sql);
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
        return true;
    }

    /*
     * Método genérico para alterar.
     */
    public boolean alterar(String sql) {
        try {
            Statement st = (Statement) c.createStatement();
            st.executeUpdate(sql);
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
        return true;
    }
    /*
     * Método para realizar backup de toda base de dados na pasta xampp/bin
     */

    public boolean backup() {
        try {
            String backup = "cmd.exe /c start C://Clinica_De_Protese//backup.bat";
            Runtime.getRuntime().exec(backup);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    /*
     * Método para realizar backup de toda base de dados na pasta xampp/bin
     */

    public boolean restaurarBackup() {
        try {
            String backup = "cmd.exe /c start C://Clinica_De_Protese//restaurar_backup.bat";
            Runtime.getRuntime().exec(backup);
        } catch (Exception e) {
            return false;
        }
        return true;
    }


    public String buscarNome(String sql) {
        try {
            Statement st = (Statement) c.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                return rs.getString("nome");
            }
        } catch (Exception e) {}
        return null; 
    }    
    //</editor-fold>
}